package com.lizhe.springboot_jsp_shiro.realms;

import com.lizhe.springboot_jsp_shiro.eneity.Perms;
import com.lizhe.springboot_jsp_shiro.eneity.User;
import com.lizhe.springboot_jsp_shiro.salt.MyByteSource;
import com.lizhe.springboot_jsp_shiro.service.UserService;
import com.lizhe.springboot_jsp_shiro.utils.ApplicationContextUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

import java.util.List;

/**
 * 类描述: 自定义认证realm
 *
 * @author Administrator
 * @create 2022-05-18 22:12
 */
public class CustomerRealm extends AuthorizingRealm {

    // 授权
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principal) {
        String primaryPrincipal = (String) principal.getPrimaryPrincipal();
        System.out.println("调用授权验证：" + primaryPrincipal);

        UserService userService = (UserService) ApplicationContextUtils.getBean("userService");
        User user = userService.findRolesByUsername(primaryPrincipal);
        System.out.println("USER:" + user);

        // 授权角色信息
        if (!CollectionUtils.isEmpty(user.getRoles())) {

            SimpleAuthorizationInfo simpleAuthorizationInfo = new SimpleAuthorizationInfo();
            user.getRoles().forEach(role -> {
                // 设置角色信息
                simpleAuthorizationInfo.addRole(role.getName());

                // 设置权限信息
                List<Perms> perms = userService.findPermsByRoleId(role.getId());
                System.out.println("perms:" + perms);

                if (!CollectionUtils.isEmpty(perms) && perms.get(0) != null) {
                    perms.forEach(perm -> {
                        simpleAuthorizationInfo.addStringPermission(perm.getName());
                    });
                }

            });
            // 返回所有的权限以及角色信息
            return simpleAuthorizationInfo;
        }
        return null;
    }

    // 认证
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
        //System.out.println("==============");
        String principal = (String) token.getPrincipal();

        //在工厂中获取service对象
        UserService userService = (UserService) ApplicationContextUtils.getBean("userService");

        User user = userService.findByUserName(principal);
        System.out.println("当前登录User" + user);

        if (!ObjectUtils.isEmpty(user)) {
            //返回数据库信息

            return new SimpleAuthenticationInfo(
                    user.getUsername(),
                    user.getPassword(),
                    new MyByteSource(user.getSalt()),
                    this.getName());
        }

        //System.out.println("用户名" + principal);
        //
        //String password_db = "123";
        //String username_db = "zhangsan";
        //if (username_db.equals(principal)) {
        //    return new SimpleAuthenticationInfo(principal, "123", this.getName());
        //}
        return null;
    }
}
